package com.demo.io;

import com.objectplanet.image.PngEncoder;

import java.awt.Dimension;
import java.awt.Rectangle;
import java.awt.Robot;
import java.awt.Toolkit;
import java.awt.image.BufferedImage;
import java.io.*;
import java.net.ServerSocket;
import java.net.Socket;

import javax.imageio.ImageIO;

/**
 * @ClassName SendScreenImg
 * @Description 该类实时发送截屏
 */
public class SendScreen extends Thread {
    public static int SERVERPORT = 8000;
    private ServerSocket serverSocket;
    private Robot robot;
    public Dimension screen;
    public Rectangle rect;
    public long startTime, endTime, s1, e1;
    private Socket socket;
    private boolean socketFlag = true;
    private PngEncoder pngEncoder = new PngEncoder();
    private ByteArrayOutputStream byteArrayOutputStream;


    public static void main(String[] args) {
        new SendScreen(SERVERPORT).start();
    }

    public SendScreen(int SERVERPORT) {
        try {
            serverSocket = new ServerSocket(SERVERPORT);
            serverSocket.setSoTimeout(864000000);
            robot = new Robot();
        } catch (Exception e) {
            e.printStackTrace();
        }
        screen = Toolkit.getDefaultToolkit().getScreenSize();
        rect = new Rectangle(screen);
    }

    @Override
    public void run() {
        while (socketFlag) {
            startTime = System.currentTimeMillis();
            try {
                System.out.println("等待数据传输...");
                socket = serverSocket.accept();
                // 创建压缩流对象
                DataOutputStream out = new DataOutputStream(socket.getOutputStream());
                // ZipOutputStream zip = new ZipOutputStream(new DataOutputStream(socket.getOutputStream()));
                // zip.setLevel(9);
                // zip.putNextEntry(new ZipEntry("screen.png"));
                //
                // //
                byteArrayOutputStream = new ByteArrayOutputStream();
                // ImageOutputStream imageOutputStream = ImageIO.createImageOutputStream(byteArrayOutputStream);

                // 获取屏幕图片
                BufferedImage img = robot.createScreenCapture(rect);
                s1 = System.currentTimeMillis();
                ImageIO.write(img,"png",byteArrayOutputStream);
                e1 = System.currentTimeMillis();
                System.out.println("Server Write: "+ (e1 - s1));
                out.write(byteArrayOutputStream.toByteArray());
                out.close();
                // // 设置ZipEntry对象
                // s1 = System.currentTimeMillis();
                // ImageIO.setUseCache(false);
                // ImageIO.write(img, "png", imageOutputStream);
                // e1 = System.currentTimeMillis();
                // System.out.println(e1 - s1);
                //
                // InputStream in = new ByteArrayInputStream(byteArrayOutputStream.toByteArray());
                // BufferedInputStream bufferedInputStream = new BufferedInputStream(in);
                // // zip.write(byteArrayOutputStream.toByteArray());
                // // pngEncoder.encode(img,zip);
                // int tmp = 0;
                // while ((tmp = bufferedInputStream.read()) != -1) {
                //     zip.write(tmp);
                // }
                //
                // zip.close();

            } catch (IOException ioe) {
                System.out.println("连接断开");
            } finally {
                if (socket != null) {
                    try {
                        socket.close();
                    } catch (IOException e) {
                        e.printStackTrace();
                        System.out.println("共享发送错误.");
                    }
                }
            }
            endTime = System.currentTimeMillis();
            System.out.println("发送延时: " + (endTime - startTime) + " ms");
        }
    }

    public void close() throws IOException {
        socketFlag = false;
        if (socket != null) socket.close();
        serverSocket.close();
    }
}